import { legacy_createStore ,applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { STORETYPE,LOVELIST,FILTERLIST,INDEXTAB,NEXTBTN,PRVEBTN,ALLFLAGSHOP,INPSHOP,SHOPLIST ,PROPSTYPE,ALLTYPE,ALLLIST,SEARCHLIST} from '../Type/Store.d';



const initialState = {
    list: JSON.parse(localStorage.getItem('list') as any) || [],
    childrenList: JSON.parse(localStorage.getItem('childrenList') as any) || [],
    CopyList:[],
    SearchList:JSON.parse(localStorage.getItem('SearchList') as any) || [],
    ShopList:JSON.parse(localStorage.getItem('ShopList') as any) || [],
    loveList:JSON.parse(localStorage.getItem('loveList') as any) || [],
    AllFlag:JSON.parse(localStorage.getItem('AllFlag') as any) || false,
    SumPrice:JSON.parse(localStorage.getItem('SumPrice') as any) || 0,
}

const routes =  (state:STORETYPE = initialState, { type, payload }:ALLTYPE) => {
    
    const Set_Item = (name:string,con:PROPSTYPE[]|string[]|boolean|number)=>{
        window.localStorage.setItem(name,JSON.stringify(con))
    }
    const Sum_Price = ()=>{
        let sum = 0 ;
        state.ShopList.forEach((item,index)=>{
            if(item.inpFlag) sum += item.price * item.count;
        })
        state.SumPrice = sum;
        Set_Item('Set_Item',state.SumPrice)
    }
    switch (type) {
    // 总数据
    case ALLLIST:
        state.list = payload;
        state.childrenList = payload[0].children as PROPSTYPE[];
        state.CopyList = payload[0].children as PROPSTYPE[];
        Set_Item('list',state.list)
        Set_Item('childrenList',state.childrenList)
        return { ...state}
    // 下标
    case INDEXTAB:
        state.childrenList = state.list[payload].children as PROPSTYPE[];
        state.CopyList = state.list[payload].children as PROPSTYPE[];
        Set_Item('childrenList',state.childrenList)
        return { ...state}
    // 搜索数据
    case SEARCHLIST:
        let SearchCopy = [...state.SearchList]
        SearchCopy.unshift(payload);
        state.SearchList = SearchCopy;
        Set_Item('SearchList',state.SearchList)
        return { ...state}
    // 购物车数据
    case SHOPLIST:
        let shopFlag = state.ShopList.some(item=>item.id == payload.id);
        if(shopFlag){
            state.ShopList.forEach((item,index)=>{
                if(item.id == payload.id) item.count++;
            })
        }else{
            payload.count = 1;
            payload.inpFlag = false;
            state.ShopList.unshift(payload);
        }
        Set_Item('ShopList',state.ShopList)

        return { ...state}
    // 单选
    case INPSHOP:
        let inpShopCopy = [...state.ShopList];
        inpShopCopy.forEach((item,index)=>{
            if(item.id == payload) item.inpFlag = !item.inpFlag;
        })
        state.AllFlag = inpShopCopy.every(item=>item.inpFlag);
        state.ShopList = inpShopCopy;
        Sum_Price();
        Set_Item('ShopList',state.ShopList)
        Set_Item('AllFlag',state.AllFlag)
        return { ...state}
    // 全选
    case ALLFLAGSHOP:
        state.AllFlag = payload;
        let AllCopy = [...state.ShopList];
        AllCopy.forEach((item,index)=>{
            item.inpFlag = payload;
        })
        state.ShopList = AllCopy;
        Sum_Price();
        Set_Item('ShopList',state.ShopList)
        Set_Item('AllFlag',state.AllFlag)
        return { ...state}
    // --
    case PRVEBTN:
        let prevCopy = [...state.ShopList];
        prevCopy.forEach((item,index)=>{
            if(item.id == payload) {
                if(item.count == 1) return;
                item.count--;
            }
        })
        state.ShopList = prevCopy;
        Sum_Price();
        Set_Item('ShopList',state.ShopList)
        return { ...state}
     // ++
    case NEXTBTN:
        let nextCopy = [...state.ShopList];
        nextCopy.forEach((item,index)=>{
            if(item.id == payload) {
                item.count++;
            }
        })
        state.ShopList = nextCopy;
        Sum_Price();
        Set_Item('ShopList',state.ShopList)
        return { ...state}
    // 筛选
    case FILTERLIST:
        let filterCopy = [...state.childrenList];
        filterCopy = state.CopyList.filter(item=>item.type == payload);
        console.log(filterCopy);
        
        state.childrenList = filterCopy
        return { ...state}

    case LOVELIST:
        let loveCopy = [...state.childrenList];
        loveCopy.forEach((item,index)=>{
            if(item.id == payload.id){
                item.flag = !item.flag;
                if(item.flag) {
                    state.loveList.unshift(item);
                }else{
                    state.loveList.forEach((v,i)=>{
                        if(v.id == payload.id) state.loveList.splice(i, 1)
                    })
                }
            }
        })
        state.childrenList = loveCopy
        Set_Item('childrenList',state.childrenList)
        Set_Item('loveList',state.loveList)
        return { ...state}

    // case ALLLIST:
    //     return { ...state}

    // case ALLLIST:
    //     return { ...state}
    
    // case ALLLIST:
    //     return { ...state}

    default:
        return { ...state}
    }
}

export default legacy_createStore(routes,applyMiddleware(thunk,logger))
